"use strict";
//自定义返回常量
const constant = {
  success: 200,
  fail: 1000,
  dbfail: 1001,
  sqlfail: 1002,
  paramfail: 1003,
  configfail: 1004,
  loginfail: 1005,
  taobaofail: 2000,
  memberfail: 2001,
  otherfail: 5000,
};

//自定义返回常量
exports.constant = { ...constant };

//成功返回
exports.success = function (data) {
  return { code: constant.success, msg: "成功", data: data };
};

//错误返回
exports.error = function (code, msg) {
  const rex = /^[1-9][0-9]{3}$/;
  if (rex.test(code)) {
    code = parseInt(code);
  } else {
    code = constant.otherfail;
  }
  if (code && msg) {
    return { code: code, msg: msg };
  }
  let reobj = { code: code };
  switch (code) {
    case constant.fail:
      reobj.msg = '失败';
      break;
    case constant.dbfail:
      reobj.msg = '数据库异常';
      break;
    case constant.sqlfail:
      reobj.msg = 'sql异常';
      break;
    case constant.paramfail:
      reobj.msg = '参数错误';
      break;
    case constant.configfail:
      reobj.msg = '配置错误';
      break;
    case constant.taobaofail:
      reobj.msg = '淘宝调用错误';
      break;
    case constant.memberfail:
      reobj.msg = '不是会员';
      break;
    default:
      reobj.msg = 'other error!';
      break;
  }
  return reobj;
};
